package com.copa.gameStore.application.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.copa.gameStore.application.CartApplicationService;
import com.copa.gameStore.client.UserClient;
import com.copa.gameStore.domain.Cart;
import com.copa.gameStore.domain.User;
import com.copa.gameStore.dto.CartDto;
import com.copa.gameStore.dto.ValidateCartDto;
import com.copa.gameStore.holder.LoginUserHolder;
import com.copa.gameStore.mapper.CartMapper;
import com.copa.gameStore.service.CartService;
import com.copa.gameStore.vo.CartVo;
import com.copa.gameStore.vo.ResponseVo;
import com.copa.gameStore.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;
import java.util.List;
import java.util.Objects;

/**
 * @author copa
 * @createDate 2022-09-27 11:32
 * @function
 */
@Service
public class CartApplicationServiceImpl implements CartApplicationService {

    @Resource
    private CartService cartService;

    @Resource
    private CartMapper cartMapper;

    @Resource
    private LoginUserHolder loginUserHolder;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> post(CartDto cartDto) {
        cartDto.setUserId(loginUserHolder.getUser().getId());
        // 这里减库存有些不妥，应该在下单时减库存
        //gameGoodsApplicationService.stock(cartDto.getGameGoodsId(), cartDto.getNum());
        cartService.post(cartDto);
        return ResponseVo.successMsg("添加到购物车成功~");
    }

    @Override
    public ResponseVo<CartVo> get() {
        Long userId = loginUserHolder.getUser().getId();
        return ResponseVo.success(cartService.getCartGoodsVo(userId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> delete(List<Long> ids) {
        Long userId = loginUserHolder.getUser().getId();
        cartService.delete(ids, userId);
        return ResponseVo.success("修改成功~");
    }

    @Override
    public void validateUser(ValidateCartDto validateCartDto) {
        cartService.validateUser(validateCartDto.getCartIds(),
                Objects.isNull(validateCartDto.getUserId()) ? loginUserHolder.getUser().getId() : validateCartDto.getUserId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByOrder(ValidateCartDto validateCartDto) {
        cartMapper.delete(new QueryWrapper<Cart>()
                .eq("user_id", Objects.isNull(validateCartDto.getUserId()) ? loginUserHolder.getUser().getId() : validateCartDto.getUserId())
                .in("id", validateCartDto.getCartIds()));
    }

    @Override
    public ResponseVo<CartVo> getByUserId(Long userId) {
        return ResponseVo.success(cartService.getCartGoodsVo(userId));
    }
}
